# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2022/8/22 21:47
# @Author : xyjiang1216
# @Email : jiangxuyan@nudt.edu.cn
# @File : demo.py
# @Version: OpenTSN_planner_builder 1.0
from PySide6.QtCore import Signal, QMimeData, QSize
from PySide6.QtGui import Qt, QDrag
from PySide6.QtWidgets import QApplication, QWidget, QGroupBox, QPushButton


class staticSWButton(QPushButton):

    def __init__(self, parent=None, text=''):
        super(staticSWButton, self).__init__(parent)
        self.text = text
        self.setText(text)

    def mouseMoveEvent(self, e):
        if e.buttons() != Qt.LeftButton:
            return
        mimeData = QMimeData()  # 拖拽
        mimeData.setText('add,' + self.text)
        drag = QDrag(self)
        drag.setMimeData(mimeData)
        drag.exec(Qt.MoveAction)
        self.setAcceptDrops(False)


class dynamicSWButton(QPushButton):

    def __init__(self, parent=None, text=''):
        super(dynamicSWButton, self).__init__(parent)
        self.text = text
        self.setText(text)

    # def mouseMoveEvent(self, e):
    #     if e.buttons() != Qt.LeftButton:
    #         return
    #     mimeData = QMimeData()
    #     mimeData.setText('move,' + self.text)
    #     drag = QDrag(self)
    #     drag.setMimeData(mimeData)
    #     # drag.setHotSpot(e.position().x()-self.rect().topLeft())
    #     drag.setHotSpot(e.pos() - self.rect().topLeft())
    #     # print(e.pos)
    #     # print(self.rect().topLeft())
    #     drag.exec(Qt.MoveAction)
    #     self.setAcceptDrops(False)


class MyGroupBox(QGroupBox):
    # AddSignal = pyqtSignal(list)
    # MoveSignal = pyqtSignal(list)
    AddSignal = Signal(list)
    MoveSignal = Signal(list)

    def __init__(self, parent):
        super(MyGroupBox, self).__init__(parent)
        self.setAcceptDrops(True)
        self.show()

    def dragEnterEvent(self, e):
        # print(e)
        if e.mimeData().hasText():
            e.accept()
        else:
            e.ignore()

    def dropEvent(self, e):
        data = e.mimeData().text().split(',')
        # print((e.pos().x(), e.pos().y()))
        if data[0] == 'add':
            self.AddSignal.emit([data[1], e.pos()])
        elif data[0] == 'move':
            self.MoveSignal.emit([data[1], e.pos()])


class DragDemoView(QWidget):

    def __init__(self):
        super(DragDemoView, self).__init__()
        self.setUI()
        self.show()

    def setUI(self):
        # self.resize(QApplication.desktop().availableGeometry().width(),
        #             QApplication.desktop().availableGeometry().height())
        self.resize(QApplication.instance().screens()[0].size(), )

        # size = QSize()
        # size.width()

        self.buttonDict = {}

        self.drawBox = MyGroupBox(self)
        self.drawBox.MoveSignal.connect(self.move_button)
        self.drawBox.AddSignal.connect(self.add_button)

        self.toolBox = QGroupBox(self)

        self.button1 = staticSWButton(self.toolBox, 'test')

        # self.drawBox.show()
        self.toolBox.setGeometry(0,
                                 0,
                                 QApplication.instance().screens()[0].size().width() / 2,
                                 QApplication.instance().screens()[0].size().height())
        self.drawBox.setGeometry(QApplication.instance().screens()[0].size().width() / 2,
                                 0,
                                 QApplication.instance().screens()[0].size().width() / 2,
                                 QApplication.instance().screens()[0].size().height())

        # self.drawBox.resize(QApplication.instance().screens()[0].size().width()/2,
        #                          QApplication.instance().screens()[0].size().height())

    def add_button(self, data):
        n = 0
        buttonName = data[0] + str(n)
        while buttonName in self.buttonDict:
            n += 1
            buttonName = data[0] + str(n)

        button = dynamicSWButton(self.drawBox, buttonName)
        self.buttonDict[buttonName] = button
        button.move(data[1])
        button.show()

    def move_button(self, data):
        button = self.buttonDict[data[0]]
        button.move(data[1])
        button.show()


if __name__ == '__main__':
    app = QApplication()
    main = DragDemoView()
    app.exit(app.exec())
